<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryTree">
              <j-category-select v-model="model.categoryTree" pcode="A01" placeholder="请选择分类" multiple />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="题目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入题目"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="选项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="options">
              <div>
                <a-button @click="addOptions" type="primary" shape="circle" icon="plus" size="small" style="margin-left: 10px;" v-if="model.options.length === 0"/>
                <div style="display: flex;align-items: center;margin-bottom: 10px;" v-for="(item, index) in model.options" :key="index">
                  <a-input v-model="item.value" placeholder="请输入序号"  style="width: 100px;margin-right: 10px;"></a-input>
                  <a-input v-model="item.label" placeholder="请输入选项"  ></a-input>
                  <a-button @click="delOptions(index)" type="danger" shape="circle" icon="minus" size="small" style="margin-left: 10px;"/>
                  <a-button @click="addOptions" type="primary" shape="circle" icon="plus" size="small" style="margin-left: 10px;"/>
                </div>
              </div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="答案" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="result">
              <a-input v-model="model.result" placeholder="请输入答案"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="radio" v-model="model.type" dictCode="question_type" placeholder="请选择类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="解析" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="info">
              <a-input type="textarea" v-model="model.info" placeholder="请输入解析"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="image">
              <j-image-upload isMultiple  v-model="model.image" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="难度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hard">
              <j-dict-select-tag type="radio" v-model="model.hard" dictCode="question_hard" placeholder="请选择难度" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'ExQuestionsForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      model:{
        type:1,
        options: []
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        categoryTree: [
          { required: true, message: '请输入分类!'},
        ],
        title: [
          { required: true, message: '请输入题目!'},
        ],
        options: [
          { required: true, message: '请输入选项!'},
        ],
        result: [
          { required: true, message: '请输入答案!'},
        ],
        type: [
          { required: true, message: '请输入类型!'},
        ],
        hard: [
          { required: true, message: '请输入难度!'},
        ],
      },
      url: {
        add: "/exam/exQuestions/add",
        edit: "/exam/exQuestions/edit",
        queryById: "/exam/exQuestions/queryById"
      }
    }
  },
  computed: {
    formDisabled(){
      return this.disabled
    },
  },
  created () {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    addOptions() {
      this.model.options.push({
        value: '',
        label: ''
      })
    },
    delOptions(index) {
      this.model.options.splice(index, 1)
    },
    add () {
      this.edit(this.modelDefault);
    },
    edit (record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm () {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if(!this.model.id){
            httpurl+=this.url.add;
            method = 'post';
          }else{
            httpurl+=this.url.edit;
            method = 'put';
          }
          httpAction(httpurl,this.model,method).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    handleCategoryChange(value,backObj){
      this.model = Object.assign(this.model, backObj);
    }
  }
}
</script>